<?php
/**
 * 制作:  叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 */
function e_debug($p="\r")
{
	if (YF_f::$arr['debug_info'])
		e_bh(implode($p, YF_f::$arr['debug_info']));
}
function d($str='', $b=true)
{
	echo $str;
	if ($b){
		e_debug();
		die;
	  }
	echo nl2br("\r"); 
}
function p($arr, $b=true)
{
	print_r($arr);
	$b && die;
	echo nl2br("\r");
}
// 输出 json
function ajax_json($arr, $b=true)
{

	if (!is_array($arr))
		$arr = array('status'=>$arr);
	if (YF_f::$config['debug']>2)
		$arr['debug'] = YF_f::$arr['debug_info'];
	
	ob_clean();
	$b && header('Content-Type: application/json; charset='. YF_f::$config['charset'].'');
	
	echo json_encode($arr);
	die;
}
// 输出 api_json
function api_json($arr)
{
	ob_clean();
	header('Content-Type: application/json; charset='. YF_f::$config['charset'].'');
			
	if (!is_array($arr))
	 {
		if ($arr===TRUE)
			$arr = array('status'=>1);
		elseif ($arr!='')
			$arr = array('status'=>0, 'emsg'=>$arr);
		else{
			$arr = $arr===false ? lang::g('Over') : '';
			$arr = array('emsg'=>$arr);
		 }	
	 }
	 
	if (YF_f::$config['debug']>2)
		$arr['debug'] = YF_f::$arr['debug_info'];
	echo json_encode($arr, JSON_FORCE_OBJECT);
	die;
}
/**
 * 生成随机数
 * @param type $l 长度
 * @param type $b 包含的字符
 * @return string
 */
function randomkeys($l=10, $b='00')
{
	$str = '0123456789ABCDEFGHJKLMNPQRSTUVWXYabcdefghjknrty';
    if ($b)
     {
        $b{0} && $str .='IOZilmopqsuvwxz'; //不易辨认
        $b{1} && $str .='!@#%^&*._+?-='; //特殊
     }
    
	$ra ='';
	$str_l = strlen($str)-1;
	for ($i=0; $i<$l; $i++)
     {
        $_k = mt_rand(0, $str_l);
        $ra .= $str{$_k};
     }
	return $ra;
}


function arr_encode($a)
{
    $a = unempty($a);
    return $a ? json_encode($a) : '';
}
function arr_decode($a)
{
    return $a ? json_decode($a, TRUE) : array();
}
/**
 * 注消数组的空变量
 */
function unempty($arr)
{
	if( !is_array($arr) )
		return $arr;
	foreach($arr as $k=>$v)
	 {
		if(is_array($v))
			$arr[$k] = unempty($v);
		if(empty($v) && $v!=='0')
			unset($arr[$k]);
	 }
	return $arr;
}

/**
 * 过虑标签 a, script, style, iframe
 * 
 * @param type $conte
 * @param type $tag 过虑的标签，默认script,style
 * @return type
 */
function preg_strip_tags($conte, $tag='script,style')
{
	$preg_tag = array('script'=>'|<script[^>]*?>.*?</script>|is',
                      'style'=>'|<style[^>]*?>.*?</style>|is',
					  'a'=>'|<[\/\!]*a[^<>]*?>|is',
                      'iframe'=>'|<iframe.*?>.*</iframe>|is');
	if (is_array($conte))
		foreach ($conte as $k=>$v)
			$conte[$k] = preg_strip_tags($v, $tag);
	else
		foreach(explode(',', $tag) as $v)
			$conte = preg_replace($preg_tag[$v], '', $conte);

	return $conte;
}

/**
 * htmlspecialchars 简写
 * 转换单双引号
 * @param type $s
 */
function htmlq($s, $b=ENT_QUOTES)
{
	return htmlspecialchars($s, $b);
}
// 输出格式后的html
function e_h($s)
{
	echo htmlspecialchars($s);
}
function e_bh($s)
{
	echo nl2br(htmlspecialchars($s));
}
/**
 * 引号" 转为 html
 * @param type $s
 */
function e_qh($s)
{
	echo str_replace('"', '&quot;', $s);//  &#039;
}
function e_a($s)
{
	echo addslashes($s);
}


function intval_ids($ids)
{
	if($ids=='')
		return '';
	if(!is_array($ids))
		$ids = explode(',', $ids);
	$idA = array();
	foreach($ids as $v)
	 {
		$id = intval($v);
		$idA[$id] = $id;
	 }
	$ids = implode(',', $idA);
	return $ids;
}
function nl2arr($str)
{
    $str = str_replace("\n", "\r", $str);
    $str = str_replace("\r\r", "\r", $str);
    return explode("\r", $str);
}

// 系统时间
function microtime_float()
{
	list($usec, $sec) = explode(" ", microtime());
	return ((float)$usec + (float)$sec);
}
/**
 * 格式时间
 * @param type $t
 * @param type $f 格式
 * @param type $d 求和
 * @return type
 */
function time_farmatStr($t, $f='Y-m-d', $d='')
{
    if (!is_numeric($t))
        $t = strtotime($t);
    if ($d)
        $t = strtotime($d, $t);
    return date($f, $t);
}
//返回两日期之间的秒数
function timeDiffer($time,$time2='')
{
	$time2 = $time2 ? strtotime( $time2) : time();
	$time = $time2 - strtotime($time);
	return $time;
}
function convertTime($s)
{
	$numpart = intval($s);
	$modifier = substr($s, -1);
	switch ($modifier) {
			case 'h':
				$numpart *= 3600;
				break;
			case 'd':
				$numpart *= 86400;
				break;
			case 'm':
				$numpart *= (86400 * 30);
				break;
			case 'y':
				$numpart *= (86400 * 365);
				break;
		}
	return $numpart;
}
function ch_week($s)
{
	$a = array('日','一','二','三','四','五','六');
	return '星期'.$a[$s];
}
function getIP()
{
    return $_SERVER['REMOTE_ADDR'];
}


/**
 * 目录树
 * @param type $sort
 * @param type $rootID
 * @param type $nk  nameKEy
 * @param type $rep 
 * @param type $pre
 * @param type $i
 * @return array
 */
function dirTree($sort, $rootID=0, $nk='name', $vk='id', $rep='. ', $pre='|-', $i=0)
{
    if (!isset($sort[$rootID]))
        return array();
    
    $res = array();
	foreach ($sort[$rootID] as $v)
      {
		if ($rep)
            $v[$nk] = str_repeat($rep, $i) . $pre . $v[$nk];
        else
            $v['grade'] = $i;

        $res[] = $v;
		if (isset($sort[$v[$vk]]))
           $res = array_merge($res, dirTree($sort, $v[$vk], $nk, $vk, $rep, $pre, $i+1));
	  }
	return $res;
}

//大写人民币
function ch_money($num)
{
	$char = array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
	$dw = array('仟','','拾','佰');
	$dw2 = array( 5=>'万',1=>'亿');

    $num = sprintf("%01.2f",$num);
    $ar = explode('.', $num);

    $retval = '';
    $start =  0;
    $int = strrev($ar[0]);
    $leng = strlen($int);
	for($i=0; $i<$leng; )
	  {
        $val = $int{$i};
        $i++;
        $start += $val;
        if(!$start)
         {
            if($i>4 && $i%8==1)
                $retval = $dw2[1] . $retval;
			
            continue;
         }

        //dw2
        if($i>4)
		  {
            if($start == $val && $i<9)
				$retval = $dw2[5];
			else	
				$retval = $dw2[$i%8] . $retval;
		  }
		//dw
        if($val)
            $retval = $dw[$i%4] . $retval;

        // 十位上1
        if($val==1 && $i==$leng && $i%4==2)
            continue;
		// 多个0
        if(!$val && $int{$i-2}=='0')
            continue;
        
        $retval = $char[$val] . $retval;
	  }
    $retval or $retval = $char[0];
    $retval .= '元';

    //小数
	if( intval($ar[1]) )
        $retval .=$char[$ar[1]{0}] .'角'. $char[$ar[1]{1}]. '分';
    else
        $retval .='整';
    
	return $retval;
}
function money($m)
{
	return round($m, 2, PHP_ROUND_HALF_DOWN);
}

/**
 * 格式化输出，带单位
 * @param type $m
 * @return type
 */
function money_f($m)
{
	return YF_f::$config['currency_symbol'] .number_format($m, 2);
}
/**
 * 格式文件大小
 */
function filesize_format($n)
{
	if($n>1000000000)
		return round( $n/1073741824, 2).' G';
	elseif($n>1000000)
		return round( $n/1048576, 2).' M';
	elseif($n>1000)
		return round( $n/1024, 2).' KB';
	else
		return $n .' B';
}

/**
 * 合法的fun名
 * 
 */
function realFunName($s)
{
	return preg_replace("|[^a-zA-Z0-9_\\\]+|", '', $s);
}
function keysToName($ids, $a)
{
    $str ='';
    foreach(explode(',', $ids) as $v)
        $str .= $a[$v]. ',';
    return $str;
}
// 合法目录名
function realFilename($str)
{
	return trim(str_replace('/', '', str_replace('\\', '', $str)), "\x00..\x1F. ");
}
function rightfulName($n)
{
   return preg_replace("|[^a-zA-Z0-9_-]+|", '', $n); 
}

// 定位数组指针	
function arr_pos_next(&$arr, $cur)
{
	reset($arr);
	while (list($k, $v) = each($arr))
		if ($cur==$k) return;
}

// 替换***
function str_xx($str, $i)
{
	$l = strlen($str);
	$l <= $i && $i=$l-1;
	$a = str_repeat('*', $i);
	$str = substr($str, $i);
	return $a.$str;
}